

var player = {
    img : playerImg, //玩家图像
    x : WIDTH/2 - 32,
    y : HEIGHT - 95,
    width :65,
    height :95,
    HP : 5,
    count: 0, //计数，用于精灵图的切换
    score:0,
    timer:0,
    init: function(canvas){
        //初始化对象
        player.HP = 5;
        player.img = playerImg1;
        player.score = 0;
        canvas.onmousemove = function(e){
            player.x = e.offsetX
            //边缘检测
            if(player.x > WIDTH - player.width) {
                player.x =  WIDTH - player.width
            }
            if(player.x < 0) {
                player.x = 0
        }
    }
    },
    draw : function(ctx){
        if( player.count < 6){
            ctx.drawImage(player.img,0,0,player.width,player.height,player.x,player.y,player.width,player.height)
        }else{
            ctx.drawImage(player.img,0,player.height,player.width,player.height,player.x,player.y,player.width,player.height)
        }

        player.count ++

        if(player.count >= 11){
            player.count = 0
        }
          
    },
    drawHP: function(ctx) {
        // 血量
        for(var i = 0; i < 5; i++) {
            // 从右往左画
            // if 如果 循环变量 i  小于 HP ，那么才画爱心， 否则 就画骷髅
            if(i < player.HP) {
              ctx.drawImage(heartImg, WIDTH - 50 * (i + 1), 20, 50, 50);
            } else {
              ctx.drawImage(skullImg, WIDTH - 50 * (i + 1), 20, 40, 40);
            }
          }
    },
    drawScore: function(ctx) {
        //  绘制玩家分数到画布上
        ctx.beginPath();
        ctx.fillStyle = '#fff';
        ctx.font = '20px 黑体';    // 设置绘制文字字体大小与字体集
        ctx.textBaseline = 'top';  //  改变绘制文字时在垂直方向上的对齐方式
        ctx.fillText('得分：'+ player.score,30, 50)  // 绘制填充文字 三个参数 要绘制的文字内容， x， y
    },

    poison(){
        // 玩家变绿
        this.img = playerPoisonImg ;
        clearTimeout(this.timer);
        this.timer = setTimeout(()=>{   
        this.img = playerImg1
        },3000)
    },
    bomb(){
        this.img = playerBombImg ;
        clearTimeout(this.timer);
        this.timer = setTimeout(()=>{   
        this.img = playerImg1
        },2)

    },

    die(canvas){
        //玩家死亡
        this.img = playerDeadImg;
        //取消canvas鼠标移动时间
        canvas.onmousemove = null;
    }

    
}